package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingLevelDto;
import com.zzyl.entity.NursingLevel;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.queryparam.NursingLevelQueryParam;
import com.zzyl.service.NursingLevelService;
import com.zzyl.vo.NursingLevelVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class NursingLevelServiceImpl implements NursingLevelService {

    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    @Override
    public PageResponse<NursingLevelVo> getPage(NursingLevelQueryParam nlqp) {
        //设置分页参数
        PageHelper.startPage(nlqp.getPageNum(), nlqp.getPageSize());

        //查询数据
        Page<NursingLevel> page = nursingLevelMapper.getPage(nlqp);

        log.info("数据库查询到的数据:{}",page.getResult());

        //数据封装
        return PageResponse.of(page, NursingLevelVo.class);
    }

    @Override
    public void addNursingLevel(NursingLevelDto nld) {
        NursingLevel nl = BeanUtil.toBean(nld, NursingLevel.class);
        nl.setCreateTime(LocalDateTime.now());
        nursingLevelMapper.addNursingLevel(nl);
    }

    @Override
    public NursingLevelVo getByIdNursingLevel(Integer id) {

        NursingLevel nl = nursingLevelMapper.getByIdNursingLevel(id);

        NursingLevelVo blv = BeanUtil.toBean(nl, NursingLevelVo.class);

        return blv;
    }

    @Override
    public void updateNursingLevel(NursingLevelDto nld) {
        NursingLevel nl = BeanUtil.toBean(nld, NursingLevel.class);

        nursingLevelMapper.updateNursingLevel(nl);

    }

    @Override
    public void statusNursingLevel(Integer id, Integer status) {

        nursingLevelMapper.statusNursingLevel(id, status);
    }

    @Override
    public void deleteNursingLevel(Integer id) {
        nursingLevelMapper.deleteNursingLevel(id);
    }

    @Override
    public List<NursingLevelVo> allList() {
        //查询所有的护理等级
        List<NursingLevel> nlLIst = nursingLevelMapper.allList();

        //VO对象集合
        List<NursingLevelVo> nlvList = new ArrayList<>();

        //把实体类对象 转换成 VO对象 存入VO对象集合
        for (NursingLevel nursingLevel : nlLIst) {

            NursingLevelVo nlv = BeanUtil.toBean(nursingLevel, NursingLevelVo.class);

            nlvList.add(nlv);
        }

        return nlvList;
    }
}
